<?php

namespace App\Http\Controllers;

use App\Http\Model\LoginModel;
use App\Http\Model\LoginTokenModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    public function login(Request $req){
        $data = $this->getReqArray($req->getContent());

        $validator = Validator::make($data,[
            "username"=>"required|between 5,10",
            "password"=>"required|between 6-10"
        ]);

        if($validator->failed()){
            $res = $this->getMsg(ERROR_CODE,LOGIN_ERROR);

            return $res;
        }

        $lm = new LoginModel();

        $loginRes = $lm->login($data['username'],$data['password']);

        $res = $this->getData($loginRes!=false?SUCCESS_CODE:ERROR_CODE,$loginRes);

        return response()->json($res);
    }

//    private function setLoginToken($id,$token){
//
//        $ltm = new LoginTokenModel();
//
//        $ltm->token = $token;
//
//        $ltm->admin_id = $id;
//
//        $id = $ltm->save();
//
//        return $id>0?SUCCESS_CODE:ERROR_CODE;
//    }
}
